package DAO;

import java.math.BigDecimal;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import Beans.Express;
import Beans.RechargeConfig;
import Utils.HibernateUtil;

public class RechargeConfigDao {

	Transaction tr = null;
	private SessionFactory sessionFactory;

	public RechargeConfigDao() {
		sessionFactory = HibernateUtil.getSessionFactory();
	}

	/**
	 * 通过配置ID返回指定配置信息
	 * 
	 * 配置ID
	 * 
	 * @return
	 */
	public RechargeConfig getConfById(int confId) {
		Session session = sessionFactory.openSession();
		tr = session.beginTransaction();
		RechargeConfig conf = (RechargeConfig) session.get(
				RechargeConfig.class, confId);
		tr.commit();
		session.close();
		return conf;
	}

	/**
	 * 删除配置
	 */
	public void delete(RechargeConfig conf) {
		Session session = sessionFactory.openSession();
		tr = session.beginTransaction();
		session.delete(conf);
		tr.commit();
		session.close();
	}

	/**
	 * 修改配置
	 */
	public void edit(RechargeConfig conf) {
		Session session = sessionFactory.openSession();
		tr = session.beginTransaction();
		session.update(conf);
		tr.commit();
		session.close();
	}

	/**
	 * 保存配置
	 * 
	 * @param conf
	 */
	public void save(RechargeConfig conf) {
		Session session = sessionFactory.openSession();
		tr = session.beginTransaction();
		session.save(conf);
		tr.commit();
		session.close();

	}

	/**
	 * 获取快递网点配置
	 * 
	 * @return
	 */
	public List<RechargeConfig> getAll(Express express) {
		Session session = sessionFactory.openSession();
		tr = session.beginTransaction();
		Criteria c = session.createCriteria(RechargeConfig.class);
		c.add(Restrictions.eq("express", express));
		c.addOrder(Order.asc("money"));
		List<RechargeConfig> confs = c.list();
		tr.commit();
		session.close();
		return confs;
	}
	
	public List<RechargeConfig> getAll(Express express,BigDecimal money) {
		Session session = sessionFactory.openSession();
		tr = session.beginTransaction();
		Criteria c = session.createCriteria(RechargeConfig.class);
		c.add(Restrictions.eq("express", express));
		c.add(Restrictions.eq("money", money));
		c.addOrder(Order.asc("money"));
		List<RechargeConfig> confs = c.list();
		tr.commit();
		session.close();
		return confs;
	}

}
